package org.white.word2pdf.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;

/**
 * @name: http 工具方法
 * @author: huifei.liu@hand-china.com
 * @create: 2018-01-06 17:02
 **/

public class HttpUtil {
    private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);
    /**
     * 发送get请求的工具方法
     * @param url
     * @param param
     * @param headerParam
     * @return
     */
    public static String sendGet(String url, Map<String, Object> param, Map<String, String> headerParam) {

        String result = "";
        if (StringUtils.isBlank(url)) {
            return result;
        }
        String equals = "=";
        BufferedReader in = null;
        StringBuilder  sb = new StringBuilder();

        //拼接get请求的url参数
        sb.append(url);
        if (!ObjectUtils.isEmpty(param)) {
            sb.append("?");
            Object tempValue = null;
            int count = 0;
            for (String key : param.keySet()) {
                //字符串类型参数
                tempValue = param.get(key);
                if (tempValue instanceof String && StringUtils.isNotBlank(tempValue.toString())) {
                    sb.append(count == 0 ? "" : "&").append(key).append(equals).append(tempValue);
                    count++;
                }
                //数组类型参数
                if (tempValue instanceof List) {
                    List conditions = (List)tempValue;
                    for (Object value : conditions) {
                        if (value instanceof String && StringUtils.isNotBlank(value.toString())) {
                            sb.append(count == 0 ? "" : "&").append(key).append(equals).append(value);
                            count++;
                        }
                    }
                }
            }
        }

        try {
            URL realUrl = new URL(sb.toString());
            HttpURLConnection connection = (HttpURLConnection)realUrl.openConnection();
            connection.setConnectTimeout(30000);
            connection.setReadTimeout(30000);
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

            //添加头部参数
            if (!ObjectUtils.isEmpty(headerParam)) {
                for (String key : headerParam.keySet()) {
                    connection.setRequestProperty(key, headerParam.get(key));
                }
            }

            connection.connect();
            Map<String, List<String>> map = connection.getHeaderFields();
            Iterator var8 = map.keySet().iterator();
            while (var8.hasNext()) {
                String key = (String)var8.next();
                logger.info(key);
            }

            String line;
            if (HttpStatus.SC_NOT_FOUND != connection.getResponseCode()) {
                for (in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
                     (line = in.readLine()) != null;
                     result = result + line) {
                }
            }

        } catch (Exception var18) {
            logger.error("发送GET请求出现异常！可能为请求接口body为空");
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception var17) {
                logger.debug(var17.getLocalizedMessage());
            }
        }
        return result;
    }

}
